/*
 * UPTorque.java
 *
 * Created on 2010-06-15, 00:31:41
 */

package JET.UtilPanels;

import JET.Centers.ControlCenter;
import JET.Frames.DataModels.DataModelsFactory;
import JET.Frames.DataModels.TorquesListModel;
import JET.Centers.Graphics.GUICenter;
import JET.MouseStuff.SpecialTasks.*;
import JET.MouseStuff.Tasks.*;
import JET.SA.SA_Dialogs;
import JET.SA.SA_Selection;
import JET.Scene;
import JET.StaticActions;
import JET.mygui.UtilTabsPane.UtilTabsPaneTab;
import JET.physics.steering.Torque;
import java.util.ArrayList;

/**
 *
 * @author 057Ry
 */
public class UPTorque extends javax.swing.JPanel implements UtilTabsPaneTab {

    static final ControlCenter CC = ControlCenter.getInstance();
    private Scene scene;

    /** Creates new form UPTorque */
    public UPTorque() {
        initComponents();
    }

    public void setScene(Scene s) {
        scene = s;
        DataModelsFactory.getTorquesListModel().setScene(scene);
    }


    public GUICenter.UtilTabs getId() {
        return GUICenter.UtilTabs.Torque;
    }

    public boolean isScrollable() {
        return true;
    }

    private TorquesListModel getTorqueListModel()
    {
        return (TorquesListModel) torqueList.getModel();
    }
    public void updateTorqueListModel()
    {
        torqueList.updateUI();
        torqueList.setSelectedIndex( getTorqueListModel().getSize()-1 );
    }

    public void setSelectedTorque(int index)
    {
        torqueList.setSelectedIndex(index);

        // TODO : udpating mass list...
    }

    public Torque getTorqueAt(int index)
    {
        if (index>=0 && index<getTorqueListModel().getSize())
            return (Torque) getTorqueListModel().getElementAt(index);
        else
            return null;
    }

    public int getTorqueListIndex(Torque torque)
    {
        for(int i=0; i<getTorqueListModel().getSize(); i++)
        {
            Torque t = (Torque)getTorqueListModel().getElementAt(i);

            if (t.equals(torque))
                return i;
        }

        return -1;
    }

    public int getSelectedTorqueIndex()
    {
        return torqueList.getSelectedIndex();
    }

    public Torque getSelectedTorque()
    {
        if ( getSelectedTorqueIndex()>=0 && getTorqueListModel().getSize()>0 )
            return (Torque) getTorqueListModel().getElementAt( getSelectedTorqueIndex() );
        else
            return null;
    }

    public double getTorqueStrengthGUIValue()
    {
        return Double.parseDouble( torqueStrengthTextField.getText() );
    }

    public String getTorqueNameGUIValue()
    {
        return torqueNameTextField.getText();
    }

    public void setTorqueNameTextFieldValue(String text)
    {
        torqueNameTextField.setText(text);
    }
    public void setTorqueStrengthTextFieldValue(String text)
    {
        torqueStrengthTextField.setText(text);
    }
    public void setTorqueActiveCheckBoxValue(boolean active)
    {
        torqueActiveCheckBox.setSelected(active);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel8 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        torqueList = new javax.swing.JList();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        addTorqueMassesButton = new javax.swing.JButton();
        removeTorqueMassesButton = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        torqueStrengthTextField = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        torqueActiveCheckBox = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        torqueNameTextField = new javax.swing.JTextField();

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("Torques list"));

        torqueList.setModel(DataModelsFactory.getTorquesListModel());
        torqueList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                torqueListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(torqueList);

        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JET/Images/green_plus.PNG"))); // NOI18N
        jButton6.setToolTipText("random velocities");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JET/Images/red_cross.PNG"))); // NOI18N
        jButton7.setToolTipText("random velocities");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton2.setText("CLL");
        jButton2.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 28, Short.MAX_VALUE)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 28, Short.MAX_VALUE)))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder("Masses list"));

        jList2.setEnabled(false);
        jScrollPane2.setViewportView(jList2);

        addTorqueMassesButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JET/Images/green_plus.PNG"))); // NOI18N
        addTorqueMassesButton.setToolTipText("random velocities");
        addTorqueMassesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTorqueMassesButtonActionPerformed(evt);
            }
        });

        removeTorqueMassesButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JET/Images/red_cross.PNG"))); // NOI18N
        removeTorqueMassesButton.setToolTipText("random velocities");
        removeTorqueMassesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeTorqueMassesButtonActionPerformed(evt);
            }
        });

        jButton3.setText("CLL");
        jButton3.setEnabled(false);
        jButton3.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton3, 0, 28, Short.MAX_VALUE)
                    .addComponent(addTorqueMassesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, Short.MAX_VALUE)
                    .addComponent(removeTorqueMassesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, Short.MAX_VALUE)))
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addComponent(addTorqueMassesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(removeTorqueMassesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
        );

        jLabel5.setText("Strength");

        torqueStrengthTextField.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        torqueStrengthTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                torqueStrengthTextFieldActionPerformed(evt);
            }
        });

        jButton4.setText("Choose Mass");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        torqueActiveCheckBox.setText("torque(s) activated");
        torqueActiveCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                torqueActiveCheckBoxItemStateChanged(evt);
            }
        });

        jLabel6.setText("Name");

        torqueNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                torqueNameTextFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(torqueNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(torqueStrengthTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                    .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(torqueActiveCheckBox)
                    .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(torqueNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(torqueStrengthTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(torqueActiveCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleName("Torque settings");
    }// </editor-fold>//GEN-END:initComponents

    private void torqueListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_torqueListValueChanged
        int[] indices = torqueList.getSelectedIndices();
        ArrayList<Torque> tList = new ArrayList<Torque>(indices.length);
        for(int i=0; i<indices.length; i++)
            tList.add( scene.getJETContext().getTorqueList().get(indices[i]) );

        SA_Dialogs.fillPropertiesDialogWithTorquesData(tList);
}//GEN-LAST:event_torqueListValueChanged

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        StaticActions.newTorque(scene);
        int torquesCount = DataModelsFactory.getTorquesListModel().getSize();
        torqueList.setSelectedIndex(torquesCount-1);
}//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed

        int[] indices = torqueList.getSelectedIndices();
        Torque[] torques = new Torque[indices.length];

        for(int i=0; i<indices.length; i++)
            torques[i] = getTorqueAt(indices[i]);

        for(int i=0; i<torques.length; i++)
            StaticActions.removeTorque(torques[i], scene);

        CC.GUIC.repaintAllSceneTabs();
}//GEN-LAST:event_jButton7ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        StaticActions.clearTorques(scene, true);
}//GEN-LAST:event_jButton2ActionPerformed

    private void addTorqueMassesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTorqueMassesButtonActionPerformed
        int[] indices = torqueList.getSelectedIndices();
        ArrayList<Torque> tList = new ArrayList<Torque>(indices.length);
        for(int i=0; i<indices.length; i++) {
            Torque t = scene.getJETContext().getTorqueList().get(indices[i]);
            tList.add(t);
        }
        if (tList.size()>0) {
            CC.MC.getMouseModeSequence().clear();
            //CC.MC.getMouseModeSequence().addTask( new HidePropertiesDialogTask() );
            CC.MC.getMouseModeSequence().addTask( new SelectMassesTask(scene) );
            CC.MC.getMouseModeSequence().addTask( new BindMassesToTorquesTask(tList, scene) );
            //CC.MC.getMouseModeSequence().addTask( new ShowPropertiesDialogTask() );
            CC.MC.getMouseModeSequence().addTask( new DoNothingTask() );
            CC.MC.getMouseModeSequence().begin();
        }
}//GEN-LAST:event_addTorqueMassesButtonActionPerformed

    private void removeTorqueMassesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeTorqueMassesButtonActionPerformed
        int[] indices = torqueList.getSelectedIndices();
        ArrayList<Torque> tList = new ArrayList<Torque>(indices.length);
        for(int i=0; i<indices.length; i++) {
            Torque t = scene.getJETContext().getTorqueList().get(indices[i]);
            t.getMassList().clear();
        }
}//GEN-LAST:event_removeTorqueMassesButtonActionPerformed

    private void torqueStrengthTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_torqueStrengthTextFieldActionPerformed
        int[] indices = torqueList.getSelectedIndices();
        for(int i=0; i<indices.length; i++) {
            Torque t = scene.getJETContext().getTorqueList().get(indices[i]);
            t.setValue( getTorqueStrengthGUIValue() );
        }
        DataModelsFactory.getTorquesListModel().fireTorquesListChanged();
}//GEN-LAST:event_torqueStrengthTextFieldActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        int[] indices = torqueList.getSelectedIndices();
        ArrayList<Torque> tList = new ArrayList<Torque>(indices.length);
        for(int i=0; i<indices.length; i++) {
            Torque t = scene.getJETContext().getTorqueList().get(indices[i]);
            tList.add(t);
        }
        if (tList.size()>0) {
            SA_Selection.clearSelection(scene);

            CC.MC.getMouseModeSequence().clear();
            //CC.MC.getMouseModeSequence().addTask( new HidePropertiesDialogTask() );
            CC.MC.getMouseModeSequence().addTask( new SelectMassesTask(scene) );
            CC.MC.getMouseModeSequence().addTask( new SetTorquesSourceMassTask(tList, scene) );
            //CC.MC.getMouseModeSequence().addTask( new ShowPropertiesDialogTask() );
            CC.MC.getMouseModeSequence().addTask( new DoNothingTask() );
            CC.MC.getMouseModeSequence().begin();
        }
}//GEN-LAST:event_jButton4ActionPerformed

    private void torqueActiveCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_torqueActiveCheckBoxItemStateChanged
        int[] indices = torqueList.getSelectedIndices();
        for(int i=0; i<indices.length; i++) {
            Torque t = scene.getJETContext().getTorqueList().get(indices[i]);
            t.setActive( torqueActiveCheckBox.isSelected() );
        }
}//GEN-LAST:event_torqueActiveCheckBoxItemStateChanged

    private void torqueNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_torqueNameTextFieldActionPerformed
        int[] indices = torqueList.getSelectedIndices();
        if (indices.length>1)
            for(int i=0; i<indices.length; i++) {
                Torque t = scene.getJETContext().getTorqueList().get(indices[i]);
                if (i>0)
                    t.setName( getTorqueNameGUIValue()+i );
                else
                    t.setName( getTorqueNameGUIValue() );
            } else if (indices.length==1) {
                Torque t = scene.getJETContext().getTorqueList().get(indices[0]);
                t.setName( getTorqueNameGUIValue() );
            }
        DataModelsFactory.getTorquesListModel().fireTorquesListChanged();
}//GEN-LAST:event_torqueNameTextFieldActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addTorqueMassesButton;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton removeTorqueMassesButton;
    private javax.swing.JCheckBox torqueActiveCheckBox;
    private javax.swing.JList torqueList;
    private javax.swing.JTextField torqueNameTextField;
    private javax.swing.JTextField torqueStrengthTextField;
    // End of variables declaration//GEN-END:variables
}
